package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.ExerciseDao;
import cn.topevery.garden.entity.po.Exercise;
import cn.topevery.garden.entity.query.ExerciseQuery;
import cn.topevery.garden.entity.vo.ExerciseVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.basis.runtime.UserRuntime;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.entity.SqlUpdateMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 活动
 *
 * @author code-generate
 * @date 2022-01-07
 */
@Repository
public class ExerciseDaoImpl extends BaseDaoImpl<Exercise> implements ExerciseDao {
    @Override
    public <P extends DbCsp> List<Exercise> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<ExerciseVO> listPage(ExerciseQuery query) {
        String table = "t_exercise te";

        String columns = "te.*";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("te.c_id desc");
        sqlQueryMate
                .where(true, "te.c_db_status", ScriptConditionEnum.Eq, 0)
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, ExerciseVO.class);
    }

    @Override
    public Exercise getEntityByColumn(String key,String value){
        KeyValue<String> map=new KeyValue<>();
        map.key=key;
        map.value=value;
        return getEntityByColumnEqValue(map);
    }

    @Override
    public DataPager<Exercise> getPage(ExerciseQuery query) {
        String table = "t_exercise te";
        String columns = "te.*,\n" +
                "        ( SELECT tsd.c_dict_name FROM t_sys_dict tsd WHERE tsd.c_dict_code = te.c_type AND tsd.c_ns_id = 'a8b07267-e1ca-43c4-81c8-a1c79b961b05' ) typeStr,\n" +
                "        ( SELECT tsd.c_dict_name FROM t_sys_dict tsd WHERE tsd.c_dict_code = te.c_status AND tsd.c_ns_id = '157b4e7c-52ea-446f-a445-30a594684e9a' ) statusStr";
        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate.where(true, "te.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotBlank(query.beginDate), "te.c_date", ScriptConditionEnum.GreaterThanEq, query.beginDate)
                .where(StringUtils.isNotBlank(query.endDate), "te.c_date", ScriptConditionEnum.LessThanEq, query.endDate)
                .where(query.getStatus()!=null, "te.c_status", ScriptConditionEnum.Eq, query.getStatus());
        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;
        return getDataPager(sqlQueryMate, pageSize, pageIndex, Exercise.class);
    }

    @Transactional
    @Override
    public void exerciseCommit(Exercise exercise, Integer userId) {
        exercise.setDbUpdateDate(LocalDateTime.now());
        exercise.setDbUpdateId(userId);
        if (exercise.getOpType() == 1) {
            exercise.setStatus(1);
        } else {
            exercise.setStatus(2);
        }
        if (exercise.getId() == null) {
            exercise.setCreateTime(LocalDateTime.now());
            exercise.setDbStatus(false);
            exercise.setCreateId(UserRuntime.getSysUser().userId);
            insert(UserRuntime.getSysUser().userId, exercise);
        } else {
            update(UserRuntime.getSysUser().userId, exercise);
        }
    }

    @Transactional
    @Override
    public void exerciseDelete(Integer id, Integer userId) {
        SqlUpdateMate sqlUpdateMate = new SqlUpdateMate("t_exercise");
        sqlUpdateMate.setColumnValue(true, "c_db_status", 1)
                .setColumnValue(true, "c_db_update_date", LocalDateTime.now())
                .setColumnValue(true, "c_db_update_id", id)
                .where(true, "c_id", ScriptConditionEnum.Eq, id);
        run(sqlUpdateMate.getSql(), CommandType.Text, dbCommand -> {
            sqlUpdateMate.getParameters().forEach(dbCommand::addParameter);
            dbCommand.executeNonQuery();
        });
    }

    @Override
    public Exercise getExerciseById(Integer id) {
        return getEntity(id);
    }

    @Override
    public void changeStatus(Integer id, Integer status, Integer userId) {
        SqlUpdateMate sqlUpdateMate = new SqlUpdateMate("t_exercise");
        sqlUpdateMate.setColumnValue(true, "c_status", status + 1)
                .setColumnValue(true, "c_db_update_date", LocalDateTime.now())
                .setColumnValue(true, "c_db_update_id", userId)
                .where(true, "c_id", ScriptConditionEnum.Eq, id);
        run(sqlUpdateMate.getSql(), CommandType.Text, dbCommand -> {
            sqlUpdateMate.getParameters().forEach(dbCommand::addParameter);
            dbCommand.executeNonQuery();
        });
    }

}
